# -*- coding:utf-8 -*-
import sys
import json
from decimal import Decimal
import MySQLdb
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
#连接数据库
dbstr=sys.argv[1]
conn=MySQLdb.connect(host='182.92.96.120',user='zhuge',passwd='zhuge1116',db=dbstr,port=3306,charset="utf8")
cur = conn.cursor()
cur.execute('select count(*) from house_sell_gov')
#获取数据库中记录总条数
count_all=cur.fetchone()[0]
#每一次获取1000条数据
page_count=1000;
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
#打开文件，准备写
file = open("/usr/local/apps/hadoop/data/test", "w")
#for($i=0;$i<=($sell_gov_count/$page_count);$i++)
for i in range(0,count_all/page_count+1):
    cur.execute('select * from house_sell_gov limit %d,%d'%(i*page_count,(i+1)*page_count))
    results=cur.fetchall()
    for row in results:
        file.write(json.dumps(row,default=default))
	file.write("\n")
file.close()
